package com.sanrenzu.everybodytv.http;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sanrenzu.everybodytv.bean.zxb.ColumItemBean;
import com.sanrenzu.everybodytv.bean.zxb.VideoItemBean;
import com.sanrenzu.everybodytv.config.UrlConfig;

import java.util.List;
import java.util.Map;

import com.sanrenzu.everybodytv.bean.gy.LiveBean;
import com.sanrenzu.everybodytv.config.UrlConfig;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
 * Created by Administrator on 2016/9/6 0006.
 */
public class LiveHttpUtils {

    //保证http访问工具仅有一个
    private   static  volatile LiveHttpUtils sinleton;
    private Retrofit retrofit;
    private ILiveBean iLiveBean;
    private LiveHttpUtils() {
        retrofit = createRetrofit();
        iLiveBean= createILiveBean();
    }
    private ILiveBean createILiveBean() {
        ILiveBean iLiveBean =   retrofit.create(ILiveBean.class);
        return iLiveBean;
    }

    //同步锁定http访问类，保证每次仅一个，锁定后通过类的实例继续调用
    public static LiveHttpUtils getInstance(){
        if(sinleton==null){
            synchronized (HttpUtils.class){
                if (sinleton==null){
                    sinleton = new LiveHttpUtils();
                }
            }
        }
        return sinleton;
    }

    /**
     * Retrofit 实例化
     * @return
     */
    private Retrofit createRetrofit(){
        return new Retrofit.Builder()
                .baseUrl(UrlConfig.Path.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(createGson()))
                .build();
    }
    /**
     * Gson实例化
     * @return
     */
    private Gson createGson(){
        Gson gson = new GsonBuilder()
                //序列化不为空
                .serializeNulls()
                .create();
        return gson;
    }
    public void getData(Callback<LiveBean> callback){
        Call<LiveBean> call = iLiveBean.getData();
        call.enqueue(callback);
    }

}
